<?php

namespace App\Http\Controllers\Backend;

use App\Manage\AccountManage;
use App\Models\Admin;
use Illuminate\Support\Facades\Crypt;

class AccountController extends BaseController
{
    // 个人账户

    // 初始化
    public function initialize()
    {
        parent::initialize();
    }

    // 退出
    public function logout()
    {
        $result = ['token' => null, 'user' => [],];

        return $this->success($result, '退出成功！');
    }

    // 生成token
    public function token()
    {
        $admin  = new AccountManage();
        $result = $admin->token();
        if($result === false){
            return $this->error($admin->getError());
        }

        return $this->success($result);
    }

    // 登录
    public function login()
    {
        $admin = new AccountManage();
        $user  = $admin->onLogin();
        if($user === false){
            return $this->error($admin->getError());
        }

        // 返回
        $token  = Crypt::encryptString(json_encode($user, JSON_UNESCAPED_UNICODE));
        $result = ['token' => $token, 'user' => $user,];

        return $this->success($result, '登录成功！');
    }

    // 翻译
    public function translate()
    {
        $result = (new AccountManage())->translate();

        return $this->success($result);
    }

    ################################################################################################
    // 修改
    public function onEdit()
    {
        $this->throwIf(empty($this->myUser), '请登录后在操作！！', LOGIN_ERROR);

        $data = [
            //'username' => $this->getData('username', null, 'trim'),
            //'role'     => $this->getData('role', null, 'trim'),
            'nickname' => $this->getData('nickname', null, 'trim'),
            'password' => $this->getData('password', null, 'trim'),
            'mobile'   => $this->getData('mobile', null, 'trim'),
            //'status'   => $this->getData('status', 1, 'intval),
        ];

        // 检查唯一性
        $model = new Admin();
        $data  = $this->filterNull($data);
        $data  = $model->checkData($data);
        $this->throwIf($data === false, $model->getError());

        // 验证
        $result = $model->checkRules($data, 'info');
        $this->throwIf($result === false, $model->getError());

        $where  = ['id' => $this->myUser['id']];
        $result = $model->oneEdit($where, $data);
        $this->throwIf($result === false, $model->getError());

        return $this->success();
    }

    // 修改密码
    public function onPassword()
    {
        $this->throwIf(empty($this->myUser), '请登录后在操作！！', LOGIN_ERROR);

        $data = ['password' => $this->getData('password', null, 'trim'),];
        // 检查唯一性
        $model = new Admin();
        $data  = $this->filterNull($data);
        $data  = $model->checkData($data);
        $this->throwIf($data === false, $model->getError());

        $where  = ['id' => $this->myUser['id']];
        $result = $model->oneEdit($where, $data);
        $this->throwIf($result === false, $model->getError());

        return $this->success();
    }

}


